<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改事迹')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-deed-edit" th:object="${deed}">
            <input name="deedId" th:field="*{deedId}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">事迹标题：</label>
                <div class="col-sm-8">
                    <input name="deedTitle" th:field="*{deedTitle}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">封面图：</label>
                <div class="col-sm-8">
                    <input id="deedPic" name="deedPic" class="form-control" type="hidden">
                    <img id="pic" data-magnify="gallery" th:data-src="*{deedPic}" th:src="*{deedPic}"
                         style="width: 220px;height: 180px;">
                    <br>
                    <!--<button type="button" class="layui-btn" id="addBtn" >上传图片</button>-->
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">事迹内容：</label>
                <div class="col-sm-8">
                    <input  id="deedContent" name="deedContent" th:field="*{deedContent}" class="form-control" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}"  class="form-control" type="text" readonly>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
        var prefix = ctx + "module/deed";
        $("#form-deed-edit").validate({
            focusCleanup: true
        });

        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入内容',
                height : 400,
                lang : 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#deedContent").val();
            $('#editor').summernote('code', content);
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

        //上传图片
        layui.use(['upload'], function () {
            var $ = layui.jquery
                , upload = layui.upload;
            //普通图片上传
            upload.render({
                elem: '#addBtn'
                , url: ctx + 'common/uploadImage_QiNiu'
                , size: 10000
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $("#pic").attr("src", result);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#deedPic").val(res.data.src);
                }
            });
        });

        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#deedContent").val(sHTML);
               // $.operate.save(prefix + "/edit", $('#form-deed-edit').serialize());
                $.modal.close();
            }
        }
    </script>
</body>
</html>